package com.alibaba.wallstreet.uitls;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;

public class CopyOfTester {

    private static final String PASSPHRASE = "862317";

    private static final String DE_INPUT = "D:/work/海外汇丰银行业务/key_test/de/caodande/ACKSTD1.CJ071.063017352226.PGP";
    private static final String DE_OUTPUT = "D:/work/海外汇丰银行业务/key_test/jieMi/ACKSTD1.CJ071.063017352226.xml";
    private static final String DE_KEY_FILE = "D:/work/海外汇丰银行业务/key/平时测试的key/alibaba_private.skr";

    private static final String E_INPUT = "D:/work/海外汇丰银行业务/key_test/en/ACKSTD1.CJ071.063017352226.xml";
    private static final String E_OUTPUT = "D:/work/海外汇丰银行业务/key_test/de/caodande/ACKSTD1.CJ071.063017352226.PGP";
    private static final String E_KEY_FILE = "D:/work/海外汇丰银行业务/key/平时测试的key/alibaba_public.pkr";
    
    
    
    
    
    private static final String HSBC_PASSPHRASE = "838690";
    private static final String HSBC_DE_KEY_FILE = "D:/work/海外汇丰银行业务/key/平时测试的key/hsbc_private.skr";
    //这是模拟的hsbc的公钥
    private static final String HSBC_E_KEY_FILE  = "D:/work/海外汇丰银行业务/key/平时测试的key/hsbc_public.pkr";
    //HSBC的UAT公钥
//    private static final String HSBC_E_KEY_FILE  = "D:/work/海外汇丰银行业务/key/汇丰UAT公钥/HKHBAPftvu0232.asc";


    public static void testDecrypt() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName(DE_INPUT);
        p.setOutputFileName(DE_OUTPUT);
        p.setPassphrase(PASSPHRASE);
        p.setSecretKeyFileName(DE_KEY_FILE);
        System.out.println(p.decrypt());
    }

    public static void testEncrypt() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName(E_INPUT);
        p.setOutputFileName(E_OUTPUT);
        p.setPassphrase(PASSPHRASE);
        p.setPublicKeyFileName(E_KEY_FILE);
        System.out.println(p.encrypt());
    }
    
    /**
     * 用自己的私钥签名（私钥解密密码是862317）
     * 用汇丰的公钥加密
     * @throws Exception
     */
    public static void testSignEncrypt() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName(E_INPUT);
        p.setOutputFileName(E_OUTPUT);
        p.setPassphrase(PASSPHRASE);
        p.setPublicKeyFileName(HSBC_E_KEY_FILE);
        p.setSecretKeyFileName(DE_KEY_FILE);
        System.out.println(p.signEncrypt());
    }
    
    
    /**
     * 用自己的私钥签名（私钥解密密码是862317）
     * 用汇丰的公钥加密
     * @throws Exception
     */
    public static void testSignEncryptHSBC() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName("D:/work/海外汇丰银行业务/key_test/en/ACKSTD1.CJ071.063017352226.xml");
        p.setOutputFileName("D:/work/海外汇丰银行业务/key_test/de/caodande/ACKSTD1.CJ071.063017352226.PGP");
        p.setPassphrase("838690");
        p.setPublicKeyFileName("D:/work/海外汇丰银行业务/key/平时测试的key/alibaba_public.pkr");
        p.setSecretKeyFileName("D:/work/海外汇丰银行业务/key/平时测试的key/hsbc_private.skr");
        System.out.println(p.signEncrypt());
    }
    
    /**
     * 用汇丰的私钥解密,用自己的公钥验证签名
     * @throws Exception
     */
    public static void testDecryptVerifyHSBC() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName("D:/work/海外汇丰银行业务/key_test/de/caodande/ACKSTD1.CJ071.063017352226.GPG");
        p.setOutputFileName("D:/work/海外汇丰银行业务/key_test/jieMi/ACKSTD1.CJ071.063017352226.xml");
        p.setPassphrase("12345678");
        p.setSecretKeyFileName("D:/work/海外汇丰银行业务/key/平时测试的key/alibaba_private.skr");
        p.setPublicKeyFileName("D:/work/海外汇丰银行业务/key/平时测试的key/hsbc_public.pkr");
        System.out.println(p.decryptVerify());
    }
    
    
    /**
     * 用阿里的UAT私钥进行解密用汇丰的UAT公钥验证签名
     * @throws Exception
     */
    public static void testDecryptVerifyUAT() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName("D:/work/海外汇丰银行业务/线上文件/UAT线上文件/success/ACKSTD1.CJ071.082811470301.PGP");
        p.setOutputFileName("D:/work/海外汇丰银行业务/线上文件/UAT线上文件/jiemi/ACKSTD1.CJ071.082811470301.xml");
        p.setPassphrase("alibaba_uat2015");
        p.setSecretKeyFileName("D:/work/海外汇丰银行业务/key/汇丰UAT公钥/alibaba_private_uat.pgp");
        p.setPublicKeyFileName("D:/work/海外汇丰银行业务/key/汇丰UAT公钥/hsbc_public_uat.asc");
        System.out.println(p.decryptVerifyStreamKey());
    }
    
    
    /**
     * 用阿里的UAT私钥进行解密用汇丰的UAT公钥验证签名
     * @throws Exception
     */
    public static void testDecryptVerifyPRD() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName("D:/work/海外汇丰银行业务/线上文件/wallstreet/STMTHK.CJ071.080913045339.500478748274.PGP");
        p.setOutputFileName("D:/work/海外汇丰银行业务/线上文件/wallstreet/STMTHK.CJ071.080913045339.500478748274.xml");
        p.setPassphrase("alibaba0510");
        p.setSecretKeyFileName("D:/work/海外汇丰银行业务/线上文件/wallstreet/private.gpg");
        p.setPublicKeyFileName("D:/work/海外汇丰银行业务/线上文件/wallstreet/hsbc_public.asc");
        System.out.println(p.decryptVerifyStreamKey());
    }
    
    /**
     * 用阿里的UAT私钥进行解密用汇丰的UAT公钥验证签名
     * @throws Exception
     */
    public static void testDecryptPRD() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName("D:/work/海外汇丰银行业务/线上文件/wallstreet/STMTHK.CJ071.080913045339.500478748274.PGP");
        p.setOutputFileName("D:/work/海外汇丰银行业务/线上文件/wallstreet/STMTHK.CJ071.080913045339.500478748274.xml");
        p.setPassphrase("alibaba0510");
        p.setSecretKeyFileName("D:/work/海外汇丰银行业务/线上文件/wallstreet/private.gpg");
        p.setPublicKeyFileName("D:/work/海外汇丰银行业务/线上文件/wallstreet/hsbc_public.asc");
        
        InputStream fileToDecrypt = new FileInputStream(p.getInputFileName());
        InputStream keyIn = new FileInputStream(p.getSecretKeyFileName());
        System.out.println(PGPUtil.decryptFile(fileToDecrypt, keyIn, p.getPassphrase().toCharArray(), p.getOutputFileName()));
    }
    
    /**
     * 用汇丰的私钥解密
     * @throws Exception
     */
    public static void testDecryptHsbc() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName(DE_INPUT);
        p.setOutputFileName(DE_OUTPUT);
        p.setPassphrase(HSBC_PASSPHRASE);
        p.setSecretKeyFileName(HSBC_DE_KEY_FILE);
        System.out.println(p.decrypt());
    }
    
    /**
     * 用汇丰的私钥解密,用自己的公钥验证签名
     * @throws Exception
     */
    public static void testDecryptVerifyHsbc() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName(DE_INPUT);
        p.setOutputFileName(DE_OUTPUT);
        p.setPassphrase(HSBC_PASSPHRASE);
        p.setSecretKeyFileName(HSBC_DE_KEY_FILE);
        p.setPublicKeyFileName(E_KEY_FILE);
        System.out.println(p.decryptVerify());
    }
    
    /**
     * 用汇丰的私钥解密,用自己的公钥验证签名
     * @throws Exception
     */
    public static void testDecryptVerifyMyTest() throws Exception {
        PGPFileProcessor p = new PGPFileProcessor();
        p.setInputFileName("D:\\alibaba\\wallstreet\\biz\\core\\src\\test\\resources\\req_encrypt\\123456_20150814220750.PGP");
        p.setOutputFileName(DE_OUTPUT);
        p.setPassphrase(HSBC_PASSPHRASE);
        p.setSecretKeyFileName(HSBC_DE_KEY_FILE);
        p.setPublicKeyFileName(E_KEY_FILE);
        System.out.println(p.decryptVerify());
    }
    
   /** 
     // 只测试加密和解密，ok
    public static void main(String[] args) {
        try {
            testEncrypt();
            testDecrypt();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    **/
    
 
    /**
    public static void main(String[] args) {
        try {
          //这三个是完全可用的
          testSignEncrypt();
          testDecryptVerifyHsbc();
          testSignEncryptHSBC();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }**/
    
    /**
    public static void main(String[] args) {
        try {
            long time1 = System.currentTimeMillis();
            System.out.println(new Date().toString());
            testSignEncrypt();
            long time2 = System.currentTimeMillis();
            System.out.println(time2 - time1);
            System.out.println(new Date().toString());

            time1 = System.currentTimeMillis();
            System.out.println(new Date().toString());
            testDecryptVerifyHsbc();   
            time2 = System.currentTimeMillis();
            System.out.println(time2 - time1);
            System.out.println(new Date().toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    **/
 
    /**
    public static void main(String[] args) {
        try {
            testDecryptVerifyUAT();
            testDecryptVerifyPRD();
            testDecryptPRD();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }**/
}